package com.zbkj.common.request.project;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.zbkj.common.enums.ProjectFunItem1Enum;
import com.zbkj.common.enums.ProjectFunItem2Enum;
import com.zbkj.common.enums.ProjectFunTypeEnum;
import com.zbkj.common.enums.ProjectTypeEnum;
import com.zbkj.common.exception.CommonException;
import com.zbkj.common.model.project.EstimateFun;
import com.zbkj.common.model.project.ProjectInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @Auther:
 * @Date: 2024/9/14 00:01
 * @Description:
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "AdminEstimateFunDetailSaveRequest对象", description = "服务端估算功能点明细编辑保存请求对象")
public class AdminEstimateFunDetailSaveRequest implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("功能点明细ID")
    private Long id;

    @ApiModelProperty("来源 0 解析ai结果 1 手工添加")
    private Integer sourceType;

    @ApiModelProperty("子系统名称")
    private String subsysName;

    @ApiModelProperty("模块名称")
    private String moduleName;

    @ApiModelProperty("功能单元名称")
    private String funName;

    @ApiModelProperty("功能类型")
    private String funType;

    //@ApiModelProperty("功能点数")
    //private String funPoints;

    @ApiModelProperty("功能点描述")
    private String funDescr;

    @ApiModelProperty("重要程度选项值 0 无效类型 非0为选项值")
    private Integer itme1;
    @ApiModelProperty("修改类型选项值 0 无效类型 非0为选项值")
    private Integer itme2;

    @ApiModelProperty("原文索引")
    private Integer idx;
    @ApiModelProperty("原文位置")
    private String idxLoc;

    /*@ApiModelProperty("新增时：当前记录下一条记录顺序号，最后一条添加不传")
    private Integer sortNum;*/

    public static void addParamCheck(Integer projectType, List<AdminEstimateFunDetailSaveRequest> funList) {
        if(CollectionUtils.isNotEmpty(funList)) {
            for(AdminEstimateFunDetailSaveRequest vo : funList) {
                if(Objects.nonNull(vo.getId())) {
                    throw new CommonException("新增记录主键值赋值错误");
                }
                //非空参数检查
                AdminEstimateFunDetailSaveRequest.paramEmptCheck(projectType, vo);
            }
        }
    }

    public static void editParamCheck(Integer projectType, List<AdminEstimateFunDetailSaveRequest> funList) {
        if(CollectionUtils.isNotEmpty(funList)) {
            for(AdminEstimateFunDetailSaveRequest vo : funList) {
                if(Objects.isNull(vo.getId())) {
                    throw new CommonException("编辑明细记录缺少主键ID参数");
                }
                if(Objects.isNull(vo.getSourceType())) {
                    throw new CommonException("编辑明细记录缺少来源类型参数");
                }
                //非空参数检查
                AdminEstimateFunDetailSaveRequest.paramEmptCheck(projectType, vo);
            }
        }
    }

    public static void paramEmptCheck(Integer projectType, AdminEstimateFunDetailSaveRequest vo) {
        if(StringUtils.isBlank(vo.getSubsysName())) {
            throw new CommonException("子系统名称不能为空");
        }
        if(StringUtils.isBlank(vo.getModuleName())) {
            throw new CommonException("模块名称不能为空");
        }
        if(StringUtils.isBlank(vo.getFunName())) {
            throw new CommonException("功能单元名称不能为空");
        }
        if(StringUtils.isBlank(vo.getFunType())) {
            throw new CommonException("功能类型不能为空");
        }
        if(Objects.isNull(vo.getItme1())) {
            throw new CommonException("重用程度不能为空");
        }
        if(ProjectTypeEnum.isType1(projectType)) {
            if(Objects.isNull(vo.getItme2())) {
                throw new CommonException("修改类型不能为空");
            }
        }
        if(StringUtils.isBlank(vo.getFunDescr())) {
            throw new CommonException("功能点描述不能为空");
        }
        if(Objects.isNull(vo.getIdx())) {
            throw new CommonException("原文索引不能为空");
        }
       /* if(StringUtils.isBlank(vo.getIdxLoc())) {
            throw new CommonException("原文位置不能为空");
        }*/
    }


    /**
     * vo转换Pojo对象批量
     * @param flag 0 新增明细 1编辑明细操作
     * @param projectType
     * @param adminUid
     * @param saveRequest
     * @param requestFunList
     */
    public static List<EstimateFun> voToPojoBatch(int flag, Integer projectType, Long adminUid, AdminEstimateFunSaveRequest saveRequest, List<AdminEstimateFunDetailSaveRequest> requestFunList) {
        List<EstimateFun> funList = Lists.newArrayList();
        if(CollectionUtils.isEmpty(requestFunList)) {
            return funList;
        }
        for(AdminEstimateFunDetailSaveRequest funReq : requestFunList) {
            funList.add(AdminEstimateFunDetailSaveRequest.voToPojo(flag,projectType,adminUid,saveRequest,funReq));
        }
        return funList;
    }

    public static EstimateFun voToPojo(int flag, Integer projectType, Long adminUid, AdminEstimateFunSaveRequest saveRequest, AdminEstimateFunDetailSaveRequest funReq) {
        EstimateFun estFun = new EstimateFun();
        BeanUtils.copyProperties(funReq, estFun);
        estFun.setProjectId(saveRequest.getProjectId());
        estFun.setReqNo(saveRequest.getAireqNo());
        estFun.setCreateId(adminUid);
        //功能类型检查-设置功能点数
        ProjectFunTypeEnum.findTypeSetAndCheck(projectType,estFun.getFunType(),estFun);
        //重用程度检查赋值
        ProjectFunItem1Enum.estimateFunSetAndCheck(projectType,estFun.getItme1(),estFun);
        //修改类型检查赋值
        ProjectFunItem2Enum.estimateFunSetAndCheck(projectType,estFun.getItme2(),estFun);
        if(flag == 0) { //新增操作
            estFun.setSourceType(1);
            estFun.setModifyState(1);
        } else { //修改操作
            if(Objects.equals(funReq.getSourceType(),0)) {
                estFun.setModifyState(2);
            }
            estFun.setIdx(null);
            estFun.setIdxLoc(null);
        }
        return estFun;
    }
}
